﻿<?php require_once('./Connections/microassur.php'); ?>
<?php mysql_select_db($database_microassur, $microassur); ?>
<?php
	function strToNoAccent($var)
	{
			$var = str_replace(
			 array(
			  "à", "â", "ä", "á", "ã", "å",
			  "î", "ï", "ì", "í",
			  "ô", "ö", "ò", "ó", "õ", "ø",
			  "ù", "û", "ü", "ú",
			  "é", "è", "ê", "ë",
			  "ç", "ÿ", "ñ","'",
			  "‘","'",
			 ),
			 array(
			  '&agrave;', '&#194;', '	&#196;', '&#193;', '&#195;', '	&#197;',
			  '	&#206;', '&#207;', '&#204;', '&#205;',
			  '&#212;', '&#214;', '&#210;', '&#211;', '&#213;', '&#216;',
			  '&#217;', '&#219;', '&#220;', '&#218;',
			  '&eacute;', '&egrave;', '&#234;', '&#235;',
			  '&#199;', '&#255;', '&#241;','&#8217;',
			  '&#8217;','&#8217;',
			 ),$var);
		return $var;
	}
	function existeChamp($database_microassur, $microassur, $table, $champ, $valeurChamp){
		mysql_select_db($database_microassur, $microassur);

		$query_rs = "SELECT COUNT(*) as nombre FROM $table WHERE $champ = '$valeurChamp'";
		$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
		$row_rs = mysql_fetch_assoc($rs);
		mysql_free_result($rs);
		return $row_rs['nombre'];
	}
	function existeChamp1($database_microassur, $microassur, $table, $champ,$id,$valeurChamp){
		$identifiant="Identifiant_".$table;
		$query_rs = "SELECT COUNT(*) as nombre FROM $table WHERE $champ = '$valeurChamp' and $identifiant!=$id";
		$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
		$row_rs = mysql_fetch_assoc($rs);
		mysql_free_result($rs);
		return $row_rs['nombre'];
	}
	function existeChamp2($database_microassur, $microassur, $table, $champ,$id,$valeurChamp){
		$identifiant="Identifiant_condition_indemnisation";
		$query_rs = "SELECT COUNT(*) as nombre FROM $table WHERE $champ = '$valeurChamp' and $identifiant!=$id";
		$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
		$row_rs = mysql_fetch_assoc($rs);
		mysql_free_result($rs);
		return $row_rs['nombre'];
	}
	function isTableTechnique($database_microassur, $microassur, $identifiant_table)
	{
		$query_rs = "SELECT COUNT(*) as nombre FROM dictionnaire WHERE Identifiant_table = '$identifiant_table' AND Type_table = 'T' ";
		$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
		$row_rs = mysql_fetch_assoc($rs);
		mysql_free_result($rs);
		return $row_rs['nombre'];
	}
	function verifLibelle($val)
	{
		if(isset($_POST["$val"])&& ($_POST["$val"]!=NULL))
			return TRUE;
		else 
			return FALSE;
	}
	function zeroDevant($val)
	{
		if(strlen($val)==1)
			$val="0".$val; 
		return $val;
	}
	function tailleDateAnnee($val)
	{
		if(strlen($val)==4) 
			return true;
		else
			return false;
	}
	function dateEN2FR($date)
	{
		if(isset($date) && $date!=NULL)
		{
			$date = str_replace('/','-',$date);
			$date = explode('-', $date);
			$date02=zeroDevant($date[2]);
			$date01=zeroDevant($date[1]);
			$date00=zeroDevant($date[0]);
			$dateFr = $date02.'-'.$date01.'-'.$date00;
			return $dateFr;
		}
		else
		return NULL;
	}
	function verifDate($date1,$date2)
	{
		if(isset($date1))
		{
			if($date1!="")
			{
			if($date2=="") $date2="00-00-0000";
				$dat1 = str_replace('/','-',$date1);
				$dat2 = str_replace('/','-',$date2);
				$date1 = explode('-', str_replace(' ','',$dat1));
				$date2 = explode('-', str_replace(' ','',$dat2));
					if(is_numeric(@$date2[0]) && is_numeric(@$date1[0]) && is_numeric(@$date2[1]) && is_numeric(@$date1[1]) && is_numeric(@$date2[2]) && is_numeric(@$date1[2]) )
					{
						if(tailleDateAnnee($date2[2]) && tailleDateAnnee($date1[2]))
						{
							$date1 = checkdate(zeroDevant($date1[1]),zeroDevant($date1[0]),$date1[2]);
							if($date2[0]!="00")
							$date2 = checkdate(zeroDevant($date2[1]),zeroDevant($date2[0]),$date2[2]);
							/*************************************/
							if(!$date1)
								return false;
							/*************************************/
							if(!$date2)
								return false;
							/*************************************/
							if($date2[0]!="00")
								$test=strcmp(dateEN2FR($dat2),dateEN2FR($dat1));
							else
								$test=1;
							if($test>0)
								return true;
							else
								return false;
						}
							else
								return false;
					}
					else
						return false;
			}
			else
				return false;
		}
		else
			return false;
	}
	function checkEmail($xmail)
	{
		$pattern = "/^[a-zA-Z0-9\.\-\_]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9\-]+/";
		return preg_match( $pattern, $xmail ); 
	}
	function colorer ($val,$val2)
	{
		$cls = '<div class='.$val2.'>'.strToNoAccent($val).'</div>';
		return $cls;
	}
	function retourValForm($val)
	{
		if(isset($_POST["$val"]))
			return str_replace(' ','',$_POST["$val"]);
		elseif(isset($_GET["$val"]))
			return str_replace(' ','',$_GET["$val"]);
		else 
			return NULL;
	}
	function retourValF($val)
	{
		if(isset($val))
		{
			if($val==intval(1))
				return colorer("Supprimé avec succès !","true");
			else
				return colorer("Erreur: Contactez la DSI !","false");
		}
	}
	// Retourn la valeur que l'utilisateur avait saisi dans un champ de type texte
	function remind_text($val)
	{
	  if(isset($_POST["$val"]))
	   return str_replace(' ','',$_POST["$val"]);
	  elseif(isset($_GET["$val"]))
	   return str_replace(' ','',$_GET["$val"]);
	  else 
	   return NULL;
	}
	// Retourn la valeur que l'utilisateur avait saisi dans un champ de type select
	function remind_select($champ, $value)
	{
		if(isset($_POST[$champ]) && $_POST[$champ]==$value)
			return 'selected="selected"';
	}
	// Pour vérifier les valeurs numériques
	function verifNumeric($val)
	{
		if(is_numeric($val))
			return true;
		else
			return false;	
	}
	// Pour vérifier les valeurs nombres décimaux
	function verifDecimal($val)
	{
		if(is_numeric($val) || is_float($val))
			return true;
		else
			return false;	
	}
	$indefini = "date indéfinie";
?>